﻿using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.Common;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HPIT.RentHouse.Admin.Controllers
{
    [Authorize]
    public class PermissionsController : Controller
    {
        private readonly IPermissionsService _permissionsService;

        public PermissionsController(IPermissionsService permissionsService)
        {
            _permissionsService = permissionsService;
        }

        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        ///获取分页数据
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult GetList(int start,int length,string name)
        {
            int count = 0;
            List<PermissionDTO> list= _permissionsService.GetPageList(start, length, name, ref count);

            PageModel pageModel = new PageModel()
            {
                data = list,
                recordsTotal = count,
                recordsFiltered = count
            };
            return Json(pageModel);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddPer(PermissionDTO permissionDTO)
        {
            AjaxResult result= _permissionsService.AddPermission(permissionDTO);
            return Json(result);
        }

        public ActionResult Edit(long id)
        {
            //根据id获取权限信息，在前端页面展示
            PermissionDTO dTO = _permissionsService.Get(id);
            return View(dTO);
        }

        [HttpPost]
        public ActionResult EditPer(PermissionDTO permissionDTO)
        {
            AjaxResult result= _permissionsService.EditPermission(permissionDTO);
            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(long id)
        {
            AjaxResult result = _permissionsService.DeletePermission(id);
            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteBatch(List<long> ids)
        {
            var result = _permissionsService.DeleteBatch(ids);
            return Json(result);
        }

    }
}